#!/usr/bin/env perl
require 5.008;
use warnings;
use strict;
use Digest::MD5;
use File::Copy;

unshift(@INC, '.');
require qpdf_test_helpers;

chdir("qpdf") or die "chdir testdir failed: $!\n";

require TestDriver;

cleanup();

my $td = new TestDriver('split-pages');

my $n_tests = 42;
my $n_compare_pdfs = 2;

# sp = split-pages
my @sp_cases = (
    [11, '%d at beginning', '', '%d_split-out.zdf'],
    [11, '%d at end', '--qdf', 'split-out.zdf_%d'],
    [11, '%d in middle', '--allow-weak-crypto --encrypt u o 128 --',
     'a-%d-split-out.zdf'],
    [11, 'pdf extension', '', 'split-out.Pdf'],
    [4, 'fallback', '--pages 11-pages.pdf 1-3 minimal.pdf --', 'split-out'],
    [1, 'broken data', '--pages broken-lzw.pdf --', 'split-out.pdf',
     {$td->FILE => "broken-lzw.out", $td->EXIT_STATUS => 3}],
    );
for (@sp_cases)
{
    $n_tests += 1 + $_->[0];
}

$td->runtest("split page group > 1",
             {$td->COMMAND => "qpdf --static-id --split-pages=5 11-pages.pdf" .
                  " --verbose split-out-group.pdf"},
             {$td->FILE => "split-pages-group.out", $td->EXIT_STATUS => 0},
             $td->NORMALIZE_NEWLINES);
foreach my $f ('01-05', '06-10', '11-11')
{
    $td->runtest("check out group $f",
                 {$td->FILE => "split-out-group-$f.pdf"},
                 {$td->FILE => "split-exp-group-$f.pdf"});
}

$td->runtest("no split-pages to stdout",
             {$td->COMMAND => "qpdf --split-pages 11-pages.pdf -"},
             {$td->FILE => "split-pages-stdout.out", $td->EXIT_STATUS => 2},
             $td->NORMALIZE_NEWLINES);

$td->runtest("split page with shared resources",
             {$td->COMMAND => "qpdf --qdf --static-id --split-pages=4".
                  " shared-images.pdf split-out-shared.pdf"},
             {$td->STRING => "", $td->EXIT_STATUS => 0});
foreach my $i (qw(01-04 05-08 09-10))
{
    $td->runtest("check output ($i)",
                 {$td->FILE => "split-out-shared-$i.pdf"},
                 {$td->FILE => "shared-split-$i.pdf"});
}

$td->runtest("split page with labels",
             {$td->COMMAND => "qpdf --qdf --static-id --split-pages=6".
                  " 11-pages-with-labels.pdf split-out-labels.pdf"},
             {$td->STRING => "", $td->EXIT_STATUS => 0});
foreach my $i (qw(01-06 07-11))
{
    $td->runtest("check output ($i)",
                 {$td->COMMAND => "qpdf-test-compare split-out-labels-$i.pdf labels-split-$i.pdf"},
                 {$td->FILE => "labels-split-$i.pdf", $td->EXIT_STATUS => 0});
}

# See comments in TODO about these expected failures. Search for
# "split page with outlines".
$td->runtest("split page with outlines",
             {$td->COMMAND => "qpdf --qdf --static-id --split-pages=10".
                  " outlines-with-actions.pdf split-out-outlines.pdf"},
             {$td->STRING => "", $td->EXIT_STATUS => 0});
foreach my $i (qw(01-10 11-20 21-30))
{
    $td->runtest("check output ($i)",
                 {$td->FILE => "split-out-outlines-$i.pdf"},
                 {$td->FILE => "outlines-split-$i.pdf"},
                 $td->EXPECT_FAILURE)
}

foreach my $d (@sp_cases)
{
    my ($n, $description, $xargs, $out, $exp) = @$d;
    if (! defined $exp)
    {
        $exp = {$td->STRING => "", $td->EXIT_STATUS => 0};
    }
    $td->runtest("split pages " . $description,
                 {$td->COMMAND =>
                      "qpdf --static-id --split-pages 11-pages.pdf" .
                      " $xargs $out"},
                 $exp,
                 $td->NORMALIZE_NEWLINES);
    my $pattern = $out;
    my $nlen = length($n);
    if ($pattern =~ m/\%d/)
    {
        $pattern =~ s/\%d/\%0${nlen}d/;
    }
    elsif ($pattern =~ m/\.pdf$/i)
    {
        $pattern =~ s/(\.pdf$)/-%0${nlen}d$1/i;
    }
    else
    {
        $pattern .= "-%0${nlen}d";
    }
    for (my $i = 1; $i <= $n; ++$i)
    {
        my $actual = sprintf($pattern, $i);
        my $expected = $actual;
        $expected =~ s/split-out/split-exp/;
        $td->runtest("check output page $i ($description)",
                     {$td->COMMAND => "qpdf-test-compare $actual $expected u"},
                     {$td->FILE => $expected, $td->EXIT_STATUS => 0});
    }
}

$td->runtest("split shared font, xobject",
             {$td->COMMAND =>
                  "qpdf --static-id --qdf --no-original-object-ids" .
                  " --split-pages shared-font-xobject.pdf" .
                  " split-out-shared-font-xobject.pdf"},
             {$td->STRING => "", $td->EXIT_STATUS => 0});
foreach my $i (qw(1 2 3 4))
{
    $td->runtest("check output ($i)",
                 {$td->FILE => "split-out-shared-font-xobject-$i.pdf"},
                 {$td->FILE => "shared-font-xobject-split-$i.pdf"});
}

$td->runtest("unreferenced resources with bad token",
             {$td->COMMAND =>
                  "qpdf --qdf --static-id --split-pages=2" .
                  " --remove-unreferenced-resources=yes" .
                  " split-tokens.pdf split-out-bad-token.pdf"},
             {$td->FILE => "split-tokens-split.out", $td->EXIT_STATUS => 3},
             $td->NORMALIZE_NEWLINES);
$td->runtest("check output",
             {$td->FILE => "split-out-bad-token-1-2.pdf"},
             {$td->FILE => "split-tokens-split-1-2.pdf"});
$td->runtest("--no-warn with proxied warnings during split",
             {$td->COMMAND =>
                  "qpdf --qdf --static-id --split-pages=2" .
                  " --no-warn --remove-unreferenced-resources=yes" .
                  " split-tokens.pdf split-out-bad-token.pdf"},
             {$td->STRING => "", $td->EXIT_STATUS => 3},
             $td->NORMALIZE_NEWLINES);

$td->runtest("shared images in form xobject",
             {$td->COMMAND => "qpdf --qdf --static-id --split-pages".
                  " shared-form-images.pdf split-out-shared-form.pdf"},
             {$td->STRING => "", $td->EXIT_STATUS => 0});
foreach my $i (qw(1 2 3 4 5 6))
{
    $td->runtest("check output ($i)",
                 {$td->FILE => "split-out-shared-form-$i.pdf"},
                 {$td->FILE => "shared-form-split-$i.pdf"});
}
$td->runtest("merge for compare",
             {$td->COMMAND => "qpdf --static-id --empty --pages" .
                  " split-out-shared-form*.pdf -- a.pdf"},
             {$td->STRING => "", $td->EXIT_STATUS => 0});
$td->runtest("check output",
             {$td->FILE => "a.pdf"},
             {$td->FILE => "shared-form-images-merged.pdf"});
compare_pdfs($td, "shared-form-images.pdf", "a.pdf");

$td->runtest("shared form xobject subkey",
             {$td->COMMAND => "qpdf --qdf --static-id --split-pages".
                  " shared-form-images-xobject.pdf" .
                  " split-out-shared-form-xobject.pdf"},
             {$td->STRING => "", $td->EXIT_STATUS => 0});
foreach my $i (qw(1 2))
{
    $td->runtest("check output ($i)",
                 {$td->FILE => "split-out-shared-form-xobject-$i.pdf"},
                 {$td->FILE => "shared-form-xobject-split-$i.pdf"});
}

my @fo_resources = (['form-xobjects-no-resources', 1],
                    ['form-xobjects-some-resources1', 0],
                    ['form-xobjects-some-resources2', 0]);
foreach my $d (@fo_resources)
{
    my ($f, $compare) = @$d;
    $td->runtest("split $f",
                 {$td->COMMAND =>
                      "qpdf --empty --static-id --pages $f.pdf 1 --" .
                      " --remove-unreferenced-resources=yes a.pdf"},
                 {$td->STRING => "", $td->EXIT_STATUS => 0},
                 $td->NORMALIZE_NEWLINES);
    $td->runtest("check output ($f)",
                 {$td->COMMAND => "qpdf-test-compare a.pdf $f-out.pdf"},
                 {$td->FILE => "$f-out.pdf", $td->EXIT_STATUS => 0});
    if ($compare)
    {
        compare_pdfs($td, "$f.pdf", "a.pdf");
    }
}

cleanup();
$td->report(calc_ntests($n_tests, $n_compare_pdfs));
